package Spark2SQL

import org.apache.spark.sql.{DataFrame, Dataset, SparkSession}

/**
  * Created by Administrator on 2018/5/28.
  */
object Spark2SQLWordCount {
  def main(args: Array[String]): Unit = {
    //创建SparkSession
    val spark = SparkSession.builder()
        .appName("Spark2SQLWordCount")
          .master("local[*]")
          .getOrCreate()

    //指定从哪里读数据
    val lines: Dataset[String] = spark.read.textFile("D:\\SparkTestData\\data.txt")

    //整理数据，切分压平
    //导入隐式转换
    import spark.implicits._
    val words: Dataset[String] = lines.flatMap(_.split(" "))

    //注册视图
    words.createTempView("v_wc")

    //执行SQL
    val result: DataFrame = spark.sql("SELECT value word, COUNT(*) counts FROM v_wc GROUP BY word ORDER BY counts DESC")

    //执行Action
    result.show()

    //释放资源
    spark.stop()
  }
}
